﻿using System;
using System.ComponentModel;
using System.Data;
using System.Windows;
using ReflectionStudio.Core.Events;
namespace WPFExcelReport
{
    /// <summary>
    /// Interaction logic for ProductionOrder.xaml
    /// </summary>
    public partial class UIVERSART : ViewBase
    {
        public VVERSART TMV
        {
            get
            {
                return (VVERSART)MV;
            }
        }
        #region ==SART===========
        public static readonly DependencyProperty SARTProperty = DependencyProperty.Register("SART",
            typeof(string), typeof(UIVERSART),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnSARTChanged));
        public string SART
        {
            get
            {
                return (string)GetValue(SARTProperty);
            }
            set
            {
                SetValue(SARTProperty, value);
                OnSARTChg(value);
            }
        }
        private static void OnSARTChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UIVERSART;
            if (src != null)
            {
                src.SART = (string)e.NewValue;
            }
        }
        #endregion
        #region ==VSPED===========
        public static readonly DependencyProperty VSPEDProperty = DependencyProperty.Register("VSPED",
            typeof(string), typeof(UIVERSART),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnVSPEDChanged));
        public string VSPED
        {
            get
            {
                return (string)GetValue(VSPEDProperty);
            }
            set
            {
                SetValue(VSPEDProperty, value);
            }
        }
        private static void OnVSPEDChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UIVERSART;
            if (src != null)
            {
                src.VSPED = (string)e.NewValue;
            }
        }
        #endregion

        #region ==LIEFNR===========
        public static readonly DependencyProperty LIEFNRProperty = DependencyProperty.Register("LIEFNR",
            typeof(string), typeof(UIVERSART),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnLIEFNRChanged));
        public string LIEFNR
        {
            get
            {
                return (string)GetValue(LIEFNRProperty);
            }
            set
            {
                SetValue(LIEFNRProperty, value);
                if (IsEdit && dwVL != null)
                {
                    dwVL["VLIEFNR"] = value;
                }
            }
        }
        private static void OnLIEFNRChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UIVERSART;
            if (src != null)
            {
                src.LIEFNR = (string)e.NewValue;
            }
        }
        #endregion

        #region ==dwVL===========
        public static readonly DependencyProperty dwVLProperty = DependencyProperty.Register("dwVL",
            typeof(DataRow), typeof(UIVERSART),
            new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OdwVLChanged));
        public DataRow dwVL
        {
            get
            {
                return (DataRow)GetValue(dwVLProperty);
            }
            set
            {
                SetValue(dwVLProperty, value);
            }
        }
        private static void OdwVLChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UIVERSART;
            if (src != null)
            {
                DataRow row = (DataRow)e.NewValue;
                src.dwVL = row;
            }
        }
        #endregion

        public UIVERSART()
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return; 
         }
        /// <summary>
        /// SetProd 当前订单
        /// SetData保存所有订单，但是用于展示今日订单
        /// SetEnv保存所有订单
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public override void Window_Loaded(object sender, RoutedEventArgs e)
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            base.Window_Loaded(sender, e);
        }
       
        public virtual void OnSARTChg(string value)
        {
            
        }
       
     
        public void Insert(DataRow _dwVL)
        {
            try
            {
               TMV.InsertVERSART(_dwVL, app.company);
                TBMain.Rows.InsertAt(_dwVL, 0);
              
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        public void VLDEL(MessageInfo msg)
        {
            DataRow dwData = TBMain.NewRow();
            dwData["VMANDANT"] = app.Mandant;
            dwData["VVERSART"] = msg.P1;
            dwData["VSPED"] = msg.P2;
            ((VVERSART)MV).DeleteVERSART(dwData, 1);
        }
        public override void Save()
        {
            if (dwVL == null)
                return;
            dwVL["VAENB"] = app.User;
            dwVL["VAEND"] = vm.date;
            ((VVERSART)MV).UpdateVERSART(dwVL, app.company, 0);
        }
        public override void Copy()
        {
            if (dwVL == null)
                return;
            if (SART.Trim().Length == 0)
            {
                MessageBox.Show("请先指定运输方式");
                return;
            }
            if (VSPED.Trim().Length == 0)
            {
                MessageBox.Show("请先指定ID");
                return;
            }
            DataRow dwData = TBMain.NewRow();
            dwData.ItemArray = dwVL.ItemArray;
            dwData["VVERSART"] = SART;
            dwData["VSPED"] = VSPED;
            Insert(dwData);
            SART = "";
            VSPED = "";
        }
        
        public override void SaveCommand(object sender, RoutedEventArgs e)
        {
            Save();
            base.SaveCommand(sender, e);
        }

        public override void DeleteCommand(object sender, RoutedEventArgs e)
        {
            if (dwVL == null)
                return;
            ((VVERSART)MV).DeleteVERSART(dwVL, 1);
            base.DeleteCommand(sender, e);
        }
        public override void CopyCommand(object sender, RoutedEventArgs e)
        {
            Copy();
            base.CopyCommand(sender, e);
        }
    }
}
